package com.smart.service.impl;

import java.util.Date;
import com.smart.util.DateUtil;
import com.smart.entity.SysUser;
import com.smart.entity.SiteInspection;
import com.smart.common.util.SysUserUtils;
import com.smart.dto.AppSiteInspectionDto;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;
import com.smart.service.SiteInspectionService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**
 * @author lgb
 * @date 2020年6月22日 上午10:19:52
 * Copyright (C) 深圳工勘科技
 * @说明: 车辆巡检service
 **/
@Service
public class SiteInspectionServiceImpl extends BaseServiceImpl<SiteInspection,Integer>
          implements SiteInspectionService{

	/** 
	 * (non-Javadoc)
	 * @see:工地名称+ 巡检所属日期+ “检查”+ 巡检位置+ 巡检结果10.2 备注及图片单独展示
	 **/
	@Override
	@Transactional(rollbackFor=RuntimeException.class)
	public String appSaveSiteInspection(AppSiteInspectionDto dto) {
		SiteInspection siteIns=new SiteInspection();
		SysUser userInfo = SysUserUtils.getSysUser();
		try {
			BeanUtils.copyProperties(siteIns,dto);
			if(!CollectionUtils.isEmpty(dto.getImgs())){
				siteIns.setImgs(String.join(",",dto.getImgs()));
			}
			Date date = new Date();
			siteIns.setCreateTime(date);
			siteIns.setUpdateTime(date);
			siteIns.setSubmitId(userInfo.getId().intValue());
			siteIns.setSubmitName(userInfo.getNickName());
			this.save(siteIns);
			return DateUtil.formatLongStr(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/** 
	 * (non-Javadoc)
	 * @see: 分页查询工地巡检
	 **/
	@Override
	@Transactional(readOnly=true)
	public IPage<SiteInspection> findPage(int pageNum,int pageSize,String createTime) {
		QueryWrapper<SiteInspection> wrapper = new QueryWrapper<>();
		SysUser userInfo = SysUserUtils.getSysUser();
		if(StringUtils.isNotEmpty(createTime)){
			wrapper.gt(SiteInspection.PROP_CREATETIME,createTime+" 00:00:00");
			wrapper.le(SiteInspection.PROP_CREATETIME,createTime+" 23:59:59");
		}
		wrapper.eq("submit_id",userInfo.getId().intValue());
		wrapper.orderByDesc(SiteInspection.PROP_CREATETIME);
		return page(new Page<>(pageNum,pageSize),wrapper);
	}

	/** 
	 * (non-Javadoc)
	 * @see: 后台端分页查询
	 **/
	@Override
	@Transactional(readOnly=true)
	public IPage<SiteInspection> sysFindPage(String submitName,String createTime_start,
			String createTime_end,Integer siteId,Integer pageNum,Integer pageSize) {
		QueryWrapper<SiteInspection> wrapper = new QueryWrapper<>();
		if(StringUtils.isNotEmpty(submitName)){
			wrapper.like(SiteInspection.PROP_SUBMIT_NAME,submitName);
		}
		if(StringUtils.isNotEmpty(createTime_start)){
			wrapper.gt(SiteInspection.PROP_CREATETIME,createTime_start+" 00:00:00");
		}
		if(StringUtils.isNotEmpty(createTime_end)){
			wrapper.le(SiteInspection.PROP_CREATETIME,createTime_end+" 23:59:59");
		}
		if(siteId != null){
			wrapper.eq(SiteInspection.PROP_SITE_ID,siteId.intValue());
		}
		wrapper.orderByDesc(SiteInspection.PROP_CREATETIME);
		return page(new Page<>(pageNum,pageSize),wrapper);
	}
	
}